/**
*FileContentManager is a Java based file manager desktop application, 
*it can show, edit and manipulate the content of the files archived inside a zip.
*
*Copyright (C) 2007 
*
*Created by Camila Sanchez [http://mimix.wordpress.com/], Rafael Naufal [http://rnaufal.livejournal.com] and Rodrigo [rdomartins@gmail.com]
*
*FileContentManager is free software; you can redistribute it and/or
*modify it under the terms of the GNU Lesser General Public
*License as published by the Free Software Foundation; either
*version 2.1 of the License, or (at your option) any later version.
*
*This library is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
*Lesser General Public License for more details.
*
*You should have received a copy of the GNU Lesser General Public
*License along with FileContentManager; if not, write to the Free Software
*Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package br.com.jteam.jfcm.gui.tree;

import java.awt.Image;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.JOptionPane;

import br.com.jteam.jfcm.controller.EventDispatchService;
import br.com.jteam.jfcm.controller.event.ShowImageEvent;
import br.com.jteam.jfcm.controller.event.ShowTextEvent;
import br.com.jteam.jfcm.gui.PresentationFile;
import br.com.jteam.jfcm.gui.adapter.ShowFileContentAdapter;
import br.com.jteam.jfcm.gui.adapter.ShowFileContentAdapterException;
import br.com.jteam.jfcm.gui.adapter.ShowFileContentAdapterImpl;
import br.com.jteam.jfcm.gui.bundle.BaseBundle;
import br.com.jteam.jfcm.model.services.ShowFileContentService;
import br.com.jteam.jfcm.model.services.impl.ShowFileContentServiceImpl;
import br.com.jteam.jfcm.model.util.FileTypeUtil;

public class ZipDataImpl implements ZipData
{
	private List<ZipData> folderItems = new ArrayList<ZipData>();

	private PresentationFile<?> presentationFile;

	public ZipDataImpl(PresentationFile<?> presentationFile)
	{
		this.presentationFile = presentationFile;
	}

	public ZipDataImpl(PresentationFile<?> presentationFile, ZipData... items)
	{
		this.presentationFile = presentationFile;
		for (ZipData data : items)
		{
			folderItems.add(data);
		}
	}

	public List<ZipData> getFolderItems()
	{
		return Collections.unmodifiableList(folderItems);
	}

	public String getName()
	{
		return FileTypeUtil.extractParentNamePath(presentationFile.getName());
	}

	public boolean isFolder()
	{
		return folderItems != null && !folderItems.isEmpty();
	}

	public void select()
	{
		ShowFileContentService showFileService =
						new ShowFileContentServiceImpl();
		ShowFileContentAdapter showFileAdaper =
						new ShowFileContentAdapterImpl(showFileService);
		PresentationFile<?> preparedFile = null;
		try
		{
			preparedFile = showFileAdaper.prepareFileToView(presentationFile);
			EventDispatchService dispatchService =
							EventDispatchService.getInstance();
			if (preparedFile.isImageFile())
			{
				Image image = (Image) preparedFile.getContent();
				dispatchService.fireEvent(new ShowImageEvent(image));
			} else if (preparedFile.isTextFile())
			{
				String text = (String) preparedFile.getContent();
				dispatchService.fireEvent(new ShowTextEvent(text));
			}
		} catch (ShowFileContentAdapterException e)
		{
			JOptionPane.showMessageDialog(	null,
											BaseBundle.getValue("error.unknow.file.type"));
		}
	}
}
